const path = require('path')
const { nanoid } = require('nanoid')
const fs = require('fs')

const base64ToImg = async (src, dir) => {
	let matches = src.match(/^data:image\/(.+);base64,(.+)$/);
	let filename = nanoid() + '.' + matches[1];
	let file = path.join(dir, filename)
	fs.writeFileSync(file, matches[2], 'base64');
	return filename
};

module.exports = { base64ToImg }